﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WGM
{
    public partial class PageSetting : Page
    {
        public PageSetting()
        {
            InitializeComponent();
        }

        ClassSQL ClassSQL = new ClassSQL();

        private void button_editdb_Click(object sender, RoutedEventArgs e)
        {
            Process.Start(AppDomain.CurrentDomain.BaseDirectory + "\\Resources\\gamesdb.mdb");
        }

        private void button_editcover_Click(object sender, RoutedEventArgs e)
        {
            Process.Start(AppDomain.CurrentDomain.BaseDirectory + "\\Gamecover\\");
        }

        private void radio_editon_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.editstate = "1";
            Properties.Settings.Default.Save();
        }

        private void radio_editoff_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.editstate = "0";
            Properties.Settings.Default.Save();
        }

        private void page_setting_Loaded(object sender, RoutedEventArgs e)
        {
            string editstate = Properties.Settings.Default.editstate;
            if (editstate == "1")
            {
                radio_editon.IsChecked = true;
            }
            else
            {
                radio_editoff.IsChecked = true;
            }

            string review = Properties.Settings.Default.review;
            if (review == "1")
            {
                radio_reviewon.IsChecked = true;
            }
            else
            {
                radio_reviewoff.IsChecked = true;
            }

            string screen = Properties.Settings.Default.screen;
            if (screen == "0")
            {
                radio_screen.IsChecked = true;
            }
            else
            {
                radio_screenoff.IsChecked = true;
            }

            string ipstatus = Properties.Settings.Default.ipstatus;
            if (ipstatus == "3")
            {
                radio_ip.IsChecked = true;
            }

            if (ipstatus == "7")
            {
                radio_ipoff.IsChecked = true;
            }

            if (ipstatus == "8")
            {
                radio_week.IsChecked = true;
            }

            if (ipstatus == "31")
            {
                radio_month.IsChecked = true;
            }

            string dselection = Properties.Settings.Default.dselection;
            switch (dselection)
            {
                case "AG":
                    radio_all.IsChecked = true;
                    break;

                case "IP":
                    radio_in.IsChecked = true;
                    break;

                case "UF":
                    radio_unf.IsChecked = true;
                    break;

                case "US":
                    radio_uns.IsChecked = true;
                    break;

                case "FAV":
                    radio_com.IsChecked = true;
                    break;
            }

            string backupstate = Properties.Settings.Default.backuppath;
            if (backupstate != "NOT SET")
            {
                button_backupath.Content = "Change Backup Path";
            }

            textbox_backpath.Text = Properties.Settings.Default.backuppath;

            combobox_tcolor.Text = Properties.Settings.Default.tcolor;
            combobox_fcolor.Text = Properties.Settings.Default.tscolor;
        }

        private void button_backupath_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            System.Windows.Forms.DialogResult result = dialog.ShowDialog();

            if (dialog.SelectedPath != "")
            {
                Properties.Settings.Default.backuppath = dialog.SelectedPath;
                Properties.Settings.Default.Save();
                textbox_backpath.Text = dialog.SelectedPath;
                button_backupath.Content = "Change Backup Path";
            }
        }

        private void radio_all_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.dselection = "AG";
            Properties.Settings.Default.Save();
        }

        private void radio_in_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.dselection = "IP";
            Properties.Settings.Default.Save();
        }

        private void radio_unf_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.dselection = "UF";
            Properties.Settings.Default.Save();
        }

        private void radio_uns_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.dselection = "US";
            Properties.Settings.Default.Save();
        }

        private void radio_com_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.dselection = "FAV";
            Properties.Settings.Default.Save();
        }

        private void button_create_Click(object sender, RoutedEventArgs e)
        {
            string gamename = textbox_name.Text;
            string gametype = comboxbox_type.Text;
            string gamecompatibility = combobox_compatibility.Text;
            string gamedescription = textbox_description.Text;

            string gamecompleted = "No";
            if (switch_completed.IsChecked == true)
            {
                gamecompleted = "Yes";
            }

            if (gamename != "")
            {
                if (comboxbox_type.Text != "Type" && comboxbox_type.Text != "-----------")
                {
                    if (combobox_compatibility.Text != "Compatibility" && combobox_compatibility.Text != "--------------")
                    {
                        if (textbox_description.Text != "")
                        {
                            try
                            {
                                string gamenamesql = gamename.Replace("'", "");
                                string gamedescriptionsql = gamedescription.Replace("'", "");

                                string sql = @"INSERT INTO gamecentral (gamename, gametype, gamecompatibility, gamedescription, completed) VALUES ('" + gamenamesql + "', '" + gametype + "', '" + gamecompatibility + "', '" + gamedescriptionsql + "', '" + gamecompleted + "')";
                                ClassSQL.gamesupdate(sql);

                                label_created.Content = gamename + " is being created";

                                Thread displaychange = new Thread(WriteSave);
                                displaychange.Start(this);
                            }
                            catch (Exception ex)
                            {
                                label_created.Content = gamename + " already exist";
                                textbox_name.Text = "";
                                progressbar_create.Value = 0;
                            }
                        }
                        else
                        {
                            label_created.Content = "Please enter a game description to proceed...";
                        }
                    }
                    else
                    {
                        label_created.Content = "Please enter a game compatibility to proceed...";
                    }
                }
                else
                {
                    label_created.Content = "Please enter a game type to proceed...";
                }
            }
            else
            {
                label_created.Content = "Please enter a game name to proceed...";
            }
        }

        private void WriteSave(object obj)
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                progressbar_create.Value = 25;
            }));
            Thread.Sleep(250);
            this.Dispatcher.Invoke((Action)(() =>
            {
                progressbar_create.Value = 50;
            }));
            Thread.Sleep(250);
            this.Dispatcher.Invoke((Action)(() =>
            {
                progressbar_create.Value = 75;
            }));
            Thread.Sleep(250);
            this.Dispatcher.Invoke((Action)(() =>
            {
                progressbar_create.Value = 100;
                label_created.Content = "Game as been saved";
                textbox_name.Text = "";
                comboxbox_type.Text = "Type";
                combobox_compatibility.Text = "Compatibility";
                textbox_description.Text = "";
                switch_completed.IsChecked = false;
            }));
            Thread.Sleep(1000);
            this.Dispatcher.Invoke((Action)(() =>
            {
                progressbar_create.Value = 0;
            }));
        }

        private void button_applytheme_Click(object sender, RoutedEventArgs e)
        {
            Elysium.Theme theme = Elysium.Theme.Light;

            SolidColorBrush ContrastColor = new SolidColorBrush();
            ContrastColor.Color = Color.FromArgb(255, 255, 255, 255);

            switch (combobox_fcolor.Text)
            {
                case "Light":
                    theme = Elysium.Theme.Light;
                    Properties.Settings.Default.tscolor = "Light";
                    Properties.Settings.Default.Save();
                    break;
                case "Dark":
                    theme = Elysium.Theme.Dark;
                    Properties.Settings.Default.tscolor = "Dark";
                    Properties.Settings.Default.Save();
                    break;
            }

            switch (combobox_tcolor.SelectedIndex)
            {
                case 0:
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Blue, ContrastColor);
                    Properties.Settings.Default.tcolor = "Blue";
                    Properties.Settings.Default.Save();
                    break;
                case 1:
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Green, ContrastColor);
                    Properties.Settings.Default.tcolor = "Green";
                    Properties.Settings.Default.Save();
                    break;
                case 2:
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Lime, ContrastColor);
                    Properties.Settings.Default.tcolor = "Lime";
                    Properties.Settings.Default.Save();
                    break;
                case 3:
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Magenta, ContrastColor);
                    Properties.Settings.Default.tcolor = "Magenta";
                    Properties.Settings.Default.Save();
                    break;
                case 4:
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Mango, ContrastColor);
                    Properties.Settings.Default.tcolor = "Mango";
                    Properties.Settings.Default.Save();
                    break;
                case 5:
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Orange, ContrastColor);
                    Properties.Settings.Default.tcolor = "Orange";
                    Properties.Settings.Default.Save();
                    break;
                case 6:
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Pink, ContrastColor);
                    Properties.Settings.Default.tcolor = "Pink";
                    Properties.Settings.Default.Save();
                    break;
                case 7:
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Purple, ContrastColor);
                    Properties.Settings.Default.tcolor = "Purple";
                    Properties.Settings.Default.Save();
                    break;
                case 8:
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Red, ContrastColor);
                    Properties.Settings.Default.tcolor = "Red";
                    Properties.Settings.Default.Save();
                    break;
                case 9:
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Rose, ContrastColor);
                    Properties.Settings.Default.tcolor = "Rose";
                    Properties.Settings.Default.Save();
                    break;
                case 10:
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Sky, ContrastColor);
                    Properties.Settings.Default.tcolor = "Sky";
                    Properties.Settings.Default.Save();
                    break;
                case 11:
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Violet, ContrastColor);
                    Properties.Settings.Default.tcolor = "Violet";
                    Properties.Settings.Default.Save();
                    break;
                case 12:
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Viridian, ContrastColor);
                    Properties.Settings.Default.tcolor = "Viridian";
                    Properties.Settings.Default.Save();
                    break;
            }
        }

        private void radio_reviewon_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.review = "1";
            Properties.Settings.Default.Save();
        }

        private void radio_reviewoff_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.review = "0";
            Properties.Settings.Default.Save();
        }

        private void radio_ip_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.ipstatus = "3";
            Properties.Settings.Default.Save();
        }

        private void radio_ipoff_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.ipstatus = "7";
            Properties.Settings.Default.Save();
        }

        private void radio_week_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.ipstatus = "8";
            Properties.Settings.Default.Save();
        }

        private void radio_month_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.ipstatus = "31";
            Properties.Settings.Default.Save();
        }

        private void radio_screen_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.screen = "0";
            Properties.Settings.Default.Save();
        }

        private void radio_screenoff_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.screen = "1";
            Properties.Settings.Default.Save();
        }

        private void button_coverart_Click(object sender, RoutedEventArgs e)
        {
            string gamename = textbox_name.Text;

            var dialog = new System.Windows.Forms.OpenFileDialog();
            System.Windows.Forms.DialogResult result = dialog.ShowDialog();

            string imgpath = dialog.FileName;
            if (imgpath != "")
            {
                File.Copy(imgpath, AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + gamename + ".jpg", true);
            }

            button_coverart.IsEnabled = false;
        }
    }
}
